package core.java;

import org.apache.commons.lang3.ArrayUtils;

import java.util.*;
import java.util.function.*;

/**
 * @author ssk
 * @Title: lambad
 * @ProjectName SpringInAction
 * @Description: TODO
 * @date 19-1-8下午6:01
 */

/**
 *Predicate<T> 	T 	boolean 	用于判别一个对象。比如求一个人是否为男性
 * Consumer<T> 	T 	void 	用于接收一个对象进行处理但没有返回，比如接收一个人并打印他的名字
 * Function<T, R> 	T 	R 	转换一个对象为不同类型的对象
 * Supplier<T> 	None 	T 	提供一个对象
 * UnaryOperator<T> 	T 	T 	接收对象并返回同类型的对象
 * BinaryOperator<T> 	(T, T) 	T 	接收两个同类型的对象，并返回一个原类型对象
 */
public class lambad {
    public static void main(String args[]) {
        Integer a[]={1,2,3,6,5,4};
        Cmp cmp = new Cmp();
        Arrays.sort(a,cmp);
        System.out.println(ArrayUtils.toString(a));
        Arrays.sort(a,(c,d)->d-c);
        System.out.println(ArrayUtils.toString(a));
        Arrays.sort(a,Comparator.naturalOrder());
        System.out.println(ArrayUtils.toString(a));
        List<Integer> aList = new ArrayList<>(Arrays.asList(a));
        aList.forEach(new MyForeach());
        System.out.println("");
        aList.forEach(b->System.out.print(b+" "));
        System.out.println("");
        aList.removeIf(new MyPredicate());
        System.out.println(ArrayUtils.toString(aList.toArray()));
        aList.removeIf(b->b==2);
        System.out.println(ArrayUtils.toString(aList.toArray()));
        aList.replaceAll(new MyUnaryOperator());
        System.out.println(ArrayUtils.toString(aList.toArray()));
        aList.replaceAll(b->{
            if (b<3) return -b;
            return b;
        });
        System.out.println(ArrayUtils.toString(aList.toArray()));
        Map map =new HashMap<Integer,Integer>();
        for(int i=0;i<aList.size();i++) map.put(i,aList.get(i));
        map.forEach(new MyBiConsumer());
        System.out.println("");
        map.forEach((c,d)->System.out.print(c+"--->"+d+' '));

    }


}
class Cmp implements Comparator<Integer>{

    @Override
    public int compare(Integer o1, Integer o2) {
        return o1-o2;
    }
}

/**
 * Consumer 实现 list forEach
 */
class MyForeach implements Consumer<Integer>{

    @Override
    public void accept(Integer integer) {
        System.out.print(integer+" ");
    }
}

/**
 * 用于 removeIf list 删除元素
 */
class MyPredicate implements Predicate<Integer>{
    @Override
    public boolean test(Integer integer) {
        return integer==6;
    }
}

/**
 * 实现 replaceAll list 操作元素
 */
class MyUnaryOperator implements UnaryOperator<Integer>{
    @Override
    public Integer apply(Integer integer) {
        if (integer>3)
        return -integer;
        return integer;
    }
}

/***
 * 实现 map foreach
 */
class MyBiConsumer  implements BiConsumer<Integer,Integer>{

    @Override
    public void accept(Integer integer, Integer integer2) {
        System.out.print(integer + " ---->>  " + integer2+' ');
    }
}